package org.jeecg.modules.business.third.strategy.emsdms;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.jeecg.modules.business.strategy.service.IMesSiteService;
import org.jeecg.modules.business.third.service.IThirdCommonService;
import org.jeecg.modules.business.third.strategy.emsdms.consts.EmsSystemConst;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.dock.constant.DocumentTask;
import org.jeecg.modules.dock.entity.WmsWarehousePaperConfig;
import org.jeecg.modules.dock.entity.dto.request.HZJReturnBucketRequest;
import org.jeecg.modules.dock.entity.ems.response.EmsBaseResponseVO;
import org.jeecg.modules.dock.entity.mes.request.VerticalAndHorizontalCuttingRequestVO;
import org.jeecg.modules.dock.enums.EmsBucketStatusEnum;
import org.jeecg.modules.dock.mapper.WmsWarehousePaperConfigMapper;
import org.jeecg.modules.dock.service.EmsDockingRepeaterService;
import org.jeecg.modules.flow.sys.constant.FlowConstant;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/*
 * 项目名称:    wms-server-jeecg
 * 接口描述:    纵横剪退纸筒(纸筒) 09 ems
 * 创建人员:    zhaojun
 * 创建时间:    2023-10-23
 */
@Service(value = "r09EmsWarehouse")
@Slf4j
@RequiredArgsConstructor
public class R09EmsWarehouse implements ThirdOperateStrategy {

    private final IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;
    private final EmsDockingRepeaterService emsDockingRepeaterService;
    private final IThirdCommonService thirdCommonService;
    private final IMesSiteService iMesSiteService;
    private final WmsWarehousePaperConfigMapper paperConfigMapper;

    @Override
    public boolean operate(Map<String, Object> map) {
        AtomicBoolean result = new AtomicBoolean(true);
        try {
            Optional<WmsInReceiptTaskMaterial> wmsInReceiptTaskMaterialsOptional = thirdCommonService.getWmsInReceiptTaskMaterials(map.get("taskId").toString());
            if (wmsInReceiptTaskMaterialsOptional.isPresent()) {
                WmsInReceiptTaskMaterial taskMaterial = wmsInReceiptTaskMaterialsOptional.get();
                VerticalAndHorizontalCuttingRequestVO vo = JSONObject.parseObject(taskMaterial.getExtraText(), VerticalAndHorizontalCuttingRequestVO.class);
                HZJReturnBucketRequest request = new HZJReturnBucketRequest();
                request.setTaskCode(vo.getTaskCode());
                request.setOperateTime(new Date());
                request.setTaskType(vo.getTaskType());
                request.setOperatorName(vo.getOperatorName());
                request.setLineName(vo.getLineName());
                request.setTaskStatus(EmsBucketStatusEnum.TASK_RECEIVED.getCode());
                request.setOrderCode(vo.getOrderCode());
//                List details = vo.getDetail().stream().map(o -> (Object) o).collect(Collectors.toList());
//                request.setDetail(details);
                if (ObjectUtil.isNotNull(vo.getDetail())) {
                    List details = vo.getDetail().stream().map(o -> (Object) o).collect(Collectors.toList());
                    request.setDetail(details);
                }
//                MesSite one = iMesSiteService.getOne(new LambdaQueryWrapper<MesSite>()
//                        .eq(MesSite::getReturnMaterialSite, vo.getSite()));
                // 根据站点查询MES站点
                WmsWarehousePaperConfig paperConfig = getPaperConfig(vo.getSite(), DocumentTask.R09);
                if (ObjectUtil.isNotEmpty(paperConfig)) {
                    request.setSite(paperConfig.getMesSite());
                }
                request.setReturnSite(EmsSystemConst.PAPER_TUBE_STATION_DOWN);
                request.setDetails(vo.getDetail());
                EmsBaseResponseVO emsBaseResponseVO = emsDockingRepeaterService.hzjReturnBucket(request);
                if (ObjectUtils.isNotEmpty(emsBaseResponseVO)) {
                    if (FlowConstant.ERROR.equals(emsBaseResponseVO.getTarget())) {
                        result.set(false);
                    }
                } else {
                    result.set(false);
                }
            } else {
                result.set(false);
            }
        } catch (Exception e) {
            log.error("R09EmsWarehouse:异常，{}", e.getMessage(), e);
            result.set(false);
        }
        return result.get();
    }

    private WmsWarehousePaperConfig getPaperConfig(String site, String businessCode) {
        WmsWarehousePaperConfig config = paperConfigMapper.selectOne(new LambdaQueryWrapper<WmsWarehousePaperConfig>()
                .eq(WmsWarehousePaperConfig::getSite, site)
                .eq(WmsWarehousePaperConfig::getBusinessCode, businessCode));
        return config;
    }
}
